package com.moleq.mgpos.activities;

import java.io.File;

import com.moleq.itemsetting.adapter.FilePickerAdapter;
import com.moleq.itemsetting.models.FileData;
import com.moleq.mgpos.activities.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

public class FilePickerActivity extends Activity {

	private ListView listViewFilePicker;
	private File mCurrentDirectory; // 閿熸枻鎷风洰褰曚綅閿熸枻鎷�
	FilePickerAdapter mFileAdapter; // ListView閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷�
	private ImageView iv_cancel;
	String fileEndings[] = { "jpg" }; // 閿熸枻鎷风ず閿熸枻鎷烽敓渚ョ》鎷烽敓鏂ゆ嫹閿熸枻鎷�
	String imagePath = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.filelist);
		listViewFilePicker = (ListView) findViewById(R.id.filelist);
		listViewFilePicker.setCacheColorHint(0x00000000);
		mFileAdapter = new FilePickerAdapter(this);
		listViewFilePicker.setAdapter(mFileAdapter);
		iv_cancel = (ImageView) findViewById(R.id.iv_cancel);
		iv_cancel.setOnClickListener(onClickListener);

		Intent intent = getIntent();
		imagePath = intent.getStringExtra("imagePath");
		if (!imagePath.equals("")) {
			mCurrentDirectory = new File(imagePath);
		} else {
			mCurrentDirectory = new File(Environment
					.getExternalStorageDirectory().getAbsolutePath());
		}
		// Item閿熸枻鎷烽敓鏂ゆ嫹褰曢敓锟�
		ListView.OnItemClickListener lv2click = new ListView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView parent, View view,
					int position, long id) {
				int fid = mFileAdapter.getItemType((int) id);
				String mPath = "";
				try {
					if (fid == 1) { // 閿熶茎纭锋嫹閿熸枻鎷烽敓鏂ゆ嫹1涓洪敓渚ョ》鎷烽敓鍙綇鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓渚ョ》鎷烽敓鏂ゆ嫹鐩綍
						String s1 = mFileAdapter.getItem((int) id).name;
						if (s1.equals("Back...")) {
							mPath = mCurrentDirectory.getParent();
						} else {
							mPath = mCurrentDirectory.getPath() + "/" + s1
									+ "/";
						}
						mCurrentDirectory = new File(mPath);
						ListFile(mCurrentDirectory);
					} else { // 閿熸枻鎷烽敓鏂ゆ嫹閫夐敓鍙鎷烽敓渚ョ》鎷烽敓鏂ゆ嫹Path
						Bundle bundle = new Bundle();
						bundle.putString("path", mCurrentDirectory.getPath()
								+ "/" + mFileAdapter.getItem((int) id).name);
						bundle.putString("imagePath",
								mCurrentDirectory.getPath());
						Intent mIntent = new Intent();
						mIntent.putExtras(bundle);
						setResult(RESULT_OK, mIntent);
						FilePickerActivity.this.finish();

					}
				} catch (Exception e) {
					Toast.makeText(FilePickerActivity.this, "Can not operate.",
							Toast.LENGTH_LONG).show();
					String error = e.getMessage();
					return;
				}
			}
		};
		ListFile(mCurrentDirectory);
		listViewFilePicker.setOnItemClickListener(lv2click);
	}

	private OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.iv_cancel:
				Intent mIntent = new Intent();
				setResult(RESULT_OK, mIntent);
				FilePickerActivity.this.finish();
				break;
			}
		}

	};

	/**
	 * 閿熷彨绛规嫹閿熸枻鎷风洰褰曢敓鏂ゆ嫹閿熶茎纭锋嫹
	 * 
	 * @param aDirectory
	 */
	private void ListFile(File aDirectory) {
		mFileAdapter.clearItems();
		mFileAdapter.notifyDataSetChanged();
		listViewFilePicker.postInvalidate();
		if (!aDirectory.getPath().equals("/sdcard")) {
			FileData fd = new FileData();
			fd.name = "Back...";
			fd.type = 1;
			mFileAdapter.addItem(fd);
		}
		for (File f : aDirectory.listFiles()) {
			if (f.isDirectory()) {
				FileData fd = new FileData();
				fd.name = f.getName();
				fd.type = 1;
				mFileAdapter.addItem(fd);
			} else {
				if (checkEnds(f.getName().toLowerCase())) {
					FileData fd = new FileData();
					fd.name = f.getName();
					fd.type = 0;
					mFileAdapter.addItem(fd);
				}
			}
		}
		mFileAdapter.notifyDataSetChanged();
		listViewFilePicker.postInvalidate();
	}

	private boolean checkEnds(String checkItsEnd) {
		for (String aEnd : fileEndings) {
			if (checkItsEnd.endsWith(aEnd))
				return true;
		}
		return false;
	}
}
